<?php
class Make_payment extends Controller
{
    function __construct()
    {
        parent::__construct();
    }
    
    function index()
    {
        $this->load->helper('form','url');
        
        $this->load->library(array('form_validation','email','recaptcha'));
        
        $data = array();
        
        $data['recaptcha']=$this->recaptcha->get_html();
        
        $this->load->model('make_a_payment_model');
       
        $data['countrydropdown'] =  $this->make_a_payment_model->selectAllCountry();
        
        if(isset($_POST['submit_x']) || isset($_POST['submit']))
		{
                        $this->form_validation->set_error_delimiters('<div class="error">','</div>');
			$this->form_validation->set_rules('recaptcha_response_field', 'Captcha field', 'callback_check_captcha');
                        $this->form_validation->set_rules('name','name','required|trim|xss_clean');
			$this->form_validation->set_rules('company','company','required|trim|xss_clean');			
			$this->form_validation->set_rules('telephone','telephone','required|trim|xss_clean');			
			$this->form_validation->set_rules('email','email','required|trim|xss_clean|valid_email');			
			
            $this->form_validation->set_rules('payment_for','payment for','required|trim|xss_clean');
            
            $this->form_validation->set_rules('payment_amount','payment amount','required|trim|xss_clean');
            
           // $this->form_validation->set_rules('country','country','required|trim|xss_clean');
			
			$this->form_validation->set_message('required', 'Please input %s');
			
			$this->form_validation->set_message('valid_email', 'Please input valid %s address');
			
			if ($this->form_validation->run() == TRUE)
			{
			    $data['name'] = $this->input->post('name');
				$data['company'] = $this->input->post('company');
				$data['email'] = $this->input->post('email');
				$data['telephone'] = $this->input->post('telephone');
				$data['payment_for'] = $this->input->post('payment_for');
                $data['payment_amount'] = $this->input->post('payment_amount');
                $data['country'] = $this->input->post('payment_amount');
                $data['payment_type'] = $this->input->post('payment_type');
                
                
                $name = $this->input->post('name');
                $company = $this->input->post('company');
                $telephone = $this->input->post('telephone');
                $email = $this->input->post('email');
                $payfor = $this->input->post('payment_for');
                $country = $this->input->post('country');
                $amount = $this->input->post('payment_amount');
                $paymentType = $this->input->post('payment_type');

                if($paymentType == 1)
                {
                    $this->load->view(TEMPLATE_NAME."/front/make_a_payment_creditcard_view",$data);
                    
                }
                else
                {
                    //$data['bankdetails'] = $this->make_a_payment_model->getBAnkDetails();
                    
                    //$arr = array('name' => $name,'companyName' => $company,'telephone' =>$telephone,'email' =>$email,'payfor' =>$payfor,'country' => $country, 'amount' => $amount, 'paymentType' => $paymentType);
                
                    //$orderid = $this->make_a_payment_model->insertpayment($arr);
                    
                    //$this->make_a_payment_model->sendemailBybanktype($orderid);
                    
                    $this->load->view(TEMPLATE_NAME."/front/make_a_payment_banktransfer_view",$data);
                    
                }
			}
			else
			{
				//unset($data);
                //$data['recaptcha']=$this->recaptcha->get_html();
				$data['name'] = $this->input->post('name');
				$data['company'] = $this->input->post('company');
				$data['email'] = $this->input->post('email');
				$data['telephone'] = $this->input->post('telephone');
				$data['payment_for'] = $this->input->post('payment_for');
                $data['payment_amount'] = $this->input->post('payment_amount');
                $data['country'] = $this->input->post('country');
                $this->load->view(TEMPLATE_NAME."/front/make_a_payment_view",$data);
			}
				
		}
        else
		{
            $this->load->view(TEMPLATE_NAME."/front/make_a_payment_view",$data);
        }
    }
    
    function payment()
    {
       
        //$this->load->helper('form','url');
        
        //$this->load->library(array('form_validation','email'));
        
        //$data = array();
                  
        $this->load->model('make_a_payment_model');             
        
        if(isset($_POST['submit_payment_x']) || isset($_POST['submit_payment']))
		{
           /*
            $this->form_validation->set_error_delimiters('<div class="error">','</div>');
			//$this->form_validation->set_rules('recaptcha_response_field', 'Captcha field', 'callback_check_captcha');
            //$this->form_validation->set_rules('name','name','required|trim|xss_clean');			
			//$this->form_validation->set_rules('company','company','required|trim|xss_clean');			
			//$this->form_validation->set_rules('telephone','telephone','required|trim|xss_clean');			
			//$this->form_validation->set_rules('email','email','required|trim|xss_clean|valid_email');			
			
            //$this->form_validation->set_rules('payment_for','payment for','required|trim|xss_clean');
            
            //$this->form_validation->set_rules('payment_amount','payment amount','required|trim|xss_clean');
            
            $this->form_validation->set_rules('creditCardNumber','credit card number','required|trim|xss_clean');
            $this->form_validation->set_rules('firstName','first name','required|trim|xss_clean');
            $this->form_validation->set_rules('lastName','last name','required|trim|xss_clean');
            
            $this->form_validation->set_rules('creditCardType','card type','required|trim|xss_clean');
            
            $this->form_validation->set_rules('cvv2Number2','card verification number','required|trim|xss_clean');
             
            $this->form_validation->set_rules('cemail','confirm email','required|trim|xss_clean|valid_email');
            
            
           // $this->form_validation->set_rules('country','country','required|trim|xss_clean');
			
			$this->form_validation->set_message('required', 'Please input %s');
			
			$this->form_validation->set_message('valid_email', 'Please input valid %s address');
			
			if ($this->form_validation->run() == TRUE)
			{
			 */
			    
                $this->_confirmpayemnt();
       /*
			}
			else
			{
				//unset($data);
                //$data['recaptcha']=$this->recaptcha->get_html();
				$data['name'] = $this->input->post('name');
				$data['company'] = $this->input->post('company');
				$data['email'] = $this->input->post('email');
				$data['telephone'] = $this->input->post('telephone');
				$data['payment_for'] = $this->input->post('payment_for');
                $data['payment_amount'] = $this->input->post('payment_amount');
                $data['country'] = $this->input->post('payment_amount');
                $data['creditCardNumber'] = $this->input->post('creditCardNumber');
            
                $data['creditCardType'] = $this->input->post('creditCardType');
            
                $data['cvv2Number'] = $this->input->post('cvv2Number2');
                
                $data['expDateMonth'] = $this->input->post('expDateYear');
             
                
                
                $this->load->view(TEMPLATE_NAME."/front/make_a_payment_view",$data);
			}*/
				
		}
        else
		{
            //$this->load->view(TEMPLATE_NAME."/front/make_a_payment_view",$data);
            redirect('make-a-payment');
        } 
    }
    
    function _confirmpayemnt()
    { //redirect(register/categories);
        $this->load->model('make_a_payment_model');  
        $name = $this->input->post('name');
        $company = $this->input->post('company');
        $telephone = $this->input->post('telephone');
        $email = $this->input->post('email');
        $payfor = $this->input->post('payment_for');
        $country = $this->input->post('country');
        $amount = $this->input->post('payment_amount');
        
        $arr = array('name' => $name,'companyName' => $company,'telephone' =>$telephone,'email' =>$email,'payfor' =>$payfor,'country' => $country, 'amount' => $amount);
        
        $orderid = $this->make_a_payment_model->insertpayment($arr);
        
        set_time_limit(100);
        //$paymentType =urlencode( $_POST['paymentType']);
         $firstName =urlencode( $_POST['firstName']);
         $lastName =urlencode( $_POST['lastName']);
         $creditCardType =urlencode( $_POST['creditCardType']);
         $creditCardNumber = urlencode($_POST['creditCardNumber']);
         $expDateMonth =urlencode( $_POST['expDateMonth']);
        
         $expDateYear =urlencode( $_POST['expDateYear']);
         $cvv2Number = urlencode($_POST['cvv2Number']);
         $budget=$_POST['radio'];
        
    	if(isset($_POST['radio'])== 1)
   		{
        		
            $budgetperiod=$_POST['budgetPeriod'];
         }
         else
         {
            $budgetperiod="";
         }
         
         //$URL = "https://www.mygate.co.za/enterprise/3x0x0/ePayService.cfc?wsdl";
        
        $Mode = "0";
        $MerchantID = "79958a8d-0c7b-4038-8e2e-8948e1d678e1";
        $ApplicationID = "4b775479-a264-444c-b774-22d5521852d8";
        
        $MerchantRef = "Inv0001";
        $InvTotal = $amount;
        $Currency = "ZAR";
        
        $CreditCard = $creditCardNumber;
        $CardHolder = $firstName.' '.$lastName;
        $CVC = $cvv2Number;
        $ExpMonth = $expDateMonth;
        $ExpYear = $expDateYear;
        $CardType = $creditCardType;
        
        //A U T H O R I S E  P U R C H A S E     (A C T I O N  =  1)
        $client = new SoapClient($URL);
        $arrResults = $client->fProcess(
        'fnblive', 			//Gateway
        $MerchantID,  		//MerchantID
        $ApplicationID, 	//ApplicationID
        '1',				//Action
        '',					//TransactionIndex
        'MyGate',			//Terminal
        $Mode,				//Mode
        $MerchantRef,		//MerchantReference
        $InvTotal,			//Amount
        $Currency,			//Currency
        '',					//CashBackAmount										
        $CardType,			//CardType
        '',					//AccountType
        $CreditCard,		//CardNumber
        $CardHolder,		//CardHolder
        $CVC,				//CCVNumber
        $ExpMonth,			//ExpiryMonth
        $ExpYear,			//ExpiryYear
        $budget,			//Budget
        $budgetperiod,		//BudgetPeriod
        '',					//AuthorizationNumber
        '',					//PIN
        '',					//DebugMode
        '06',				//eCommerceIndicator							
        '',					//verifiedByVisaXID
        '',					//verifiedByVisaCAFF
        '',					//secureCodeUCAF
        '',					//Unique Client Index - this is used to uniquely identify the client and is used by the MyGate Fraud module. It is an optional parameter. Please see online documentation for details.
        '',					//IP Address - this is the IP address of the user using the online gateway (retrieved by yourselves), and is used by the MyGate Fraud module. It is an optional parameter.	 Please see online documentation for details.
        ''					//Shipping Country Code - this is the 2-digit shipping country code of the user, and is used by the MyGate Fraud module. It is an optional parameter. Please see online documentation for details.				
        );
        
        //P U R C H A S E     (A C T I O N  =  3)								
        list($ResultName, $ResultValue) = explode("||",$arrResults[0]);
        if ($ResultValue = 0)
        {
        	list($ResultName, $ResultValue) = explode("||",$arrResults[1]);
        	$arrResults = $client->fProcess(
        	'fnblive', 			//Gateway
        	$MerchantID,  		//MerchantID
        	$ApplicationID, 	//ApplicationID
        	'3',				//Action
        	$ResultValue,		//TransactionIndex
        	'',					//Terminal
        	'',					//Mode
        	'',					//MerchantReference
        	'',					//Amount
        	'',					//Currency
        	'',					//CashBackAmount										
        	'',					//CardType
        	'',					//AccountType
        	'',					//CardNumber
        	'',					//CardHolder
        	'',					//CCVNumber
        	'',					//ExpiryMonth
        	'',					//ExpiryYear
        	'',					//Budget
        	'',					//BudgetPeriod
        	'',					//AuthorizationNumber
        	'',					//PIN
        	'',					//DebugMode
        	'',					//eCommerceIndicator							
        	'',					//verifiedByVisaXID
        	'',					//verifiedByVisaCAFF
        	''					//secureCodeUCAF
        	);
        	list($ResultName, $ResultValue) = explode("||",$arrResults[0]);
        	if ($ResultValue = 0)
        	{
        	
        	$formStr = "<html>";
        	$formStr .="<head>";
        	$formStr .="<title>".'Mykprisguide'."</title>";
        	$formStr .="</head>";
        	$formStr .="<body>";
        	$formStr .="<form name=\"getBackWithDetailsFromPaypal\" id=\"getBackWithDetailsFromPaypal\" method=\"POST\" enctype=\"multipart/form-data\" action=\"".site_url()."make-a-payment/paymentsuccess\">";
        	$formStr .="<input type=\"hidden\" name=\"orderid\" value=\"".$orderid."\"/>";
        	$formStr .="<input type=\"hidden\" name=\"transID\" value=\"".$MerchantID."\" />";
        	$formStr .="<input type=\"hidden\" name=\"transactionType\" value=\"direct\" />";
        	$formStr .="</form>";
        	$formStr .="<script type=\"text/javascript\">document.getElementById('getBackWithDetailsFromPaypal').submit();</script>";
        	$formStr .="</body>";
        	$formStr .="</html>";
           	
        	echo $formStr;	 
           		
        		//echo("<br />");	
        		//echo("Successful Authorization <br />");
        		//list($ResultName, $ResultValue) = explode("||",$arrResults[0]);
        		//echo($ResultValue);
        		//echo("<br />");	
        	}
        	else
        	{
        	$formelements .="<head>";
        	$formelements .="<title>Mykprisguide</title>";
        	$formelements .="</head>";
        	$formelements .="<body>";
        	$formelements .="
        				<form name=\"frmfailure\" id=\"frmfailure\" action=\"".site_url()."make-a-payment/paymentfail\" METHOD=\"post\" >
        				<input type=\"hidden\" name=\"firstName\" value='".$_POST['firstName']."' />
        				<input type=\"hidden\" name=\"lastName\" value='".$_POST['lastName']."' />
        				<input type=\"hidden\" name=\"orderid\" value='".$orderid."'/>
        				<input type=\"hidden\" name=\"country\" value='".$_POST['country']."' />
        				<input type=\"hidden\" name=\"city\" value='".$_POST['city']."' />
        				<input type=\"hidden\" name=\"address\" value='".$_POST['address']."' />
        				<input type=\"hidden\" name=\"zip\" value='".$_POST['zip']."' />
        				<input type=\"hidden\" name=\"creditCardType\" value='".$_POST['creditCardType']."' />
        				<input type=\"hidden\" name=\"creditCardNumber\" value='".$_POST['creditCardNumber']."' />
        				<input type=\"hidden\" name=\"expDateMonth\" value='".$_POST['expDateMonth']."' />
        				<input type=\"hidden\" name=\"expDateYear\" value='".$_POST['expDateYear']."' />
        				<input type=\"hidden\" name=\"cvv2Number\" value='".$_POST['cvv2Number']."' />
        				<input type=\"hidden\" name=\"amount\" value='".$_POST['amount']."' />
        				<input type=\"hidden\" name=\"error\" value=\"Failed Authorization\" />
        				</form>
        				";
        				$formelements .="<script type=\"text/javascript\">document.getElementById('frmfailure').submit();</script>";
        	$formelements .="</body>";
        	$formelements .="</html>";
        	echo $formelements;
        	
        	}
        }
        else
        {
        	$formelements .="<head>";
        	$formelements .="<title>Mykprisguide</title>";
        	$formelements .="</head>";
        	$formelements .="<body>";
        	$formelements .="
        				<form name=\"frmfailure\" id=\"frmfailure\" action=\"".site_url()."make-a-payment/paymentfail\" METHOD=\"post\" >
        				<input type=\"hidden\" name=\"firstName\" value='".$_POST['firstName']."' />
        				<input type=\"hidden\" name=\"lastName\" value='".$_POST['lastName']."' />
        				<input type=\"hidden\" name=\"orderid\" value=\"".$orderid."\"/>
        				<input type=\"hidden\" name=\"country\" value='".$_POST['country']."' />
        				<input type=\"hidden\" name=\"city\" value='".$_POST['city']."' />
        				<input type=\"hidden\" name=\"address\" value='".$_POST['address']."' />
        				<input type=\"hidden\" name=\"zip\" value='".$_POST['zip']."' />
        				<input type=\"hidden\" name=\"creditCardType\" value='".$_POST['creditCardType']."' />
        				<input type=\"hidden\" name=\"creditCardNumber\" value='".$_POST['creditCardNumber']."' />
        				<input type=\"hidden\" name=\"expDateMonth\" value='".$_POST['expDateMonth']."' />
        				<input type=\"hidden\" name=\"expDateYear\" value='".$_POST['expDateYear']."' />
        				<input type=\"hidden\" name=\"cvv2Number\" value='".$_POST['cvv2Number']."' />
        				<input type=\"hidden\" name=\"amount\" value='".$_POST['amount']."' />
        				<input type=\"hidden\" name=\"error\" value=\"Failed Authorization\" />
        				</form>
        				";
        				$formelements .="<script type=\"text/javascript\">document.getElementById('frmfailure').submit();</script>";
        	$formelements .="</body>";
        	$formelements .="</html>";
        	echo $formelements;
        	
            }	
    }
    
    function paymentfail()
    {
        $this->load->model('make_a_payment_model');
        
        $this->make_a_payment_model->deletepaymentfail();
        
        $this->load->view(TEMPLATE_NAME."/front/make_a_paymentfail_view");
    }
    
    function paymentsuccess()
    {
        $this->load->model('make_a_payment_model');
        
        $arr = array('transaction_id ' => $this->input->post('transID'),'pay_status' => '1');
        
        $cond = array('id' => $this->input->post('orderid'));
        
        $this->make_a_payment_model->updatepaymentsuccess($arr,$cond);
        
        $this->make_a_payment_model->sendemailBybanktype($this->input->post('orderid'));
        
        $this->load->view(TEMPLATE_NAME."/front/make_a_paymentsuccess_view");
    }
        
    function check_captcha($val) 
    {
        
        $result = $this->recaptcha->check_answer($_SERVER["REMOTE_ADDR"],$this->input->post('recaptcha_challenge_field'),$val);
        if ($result) {
            return true;
        } else {
            $this->form_validation->set_message('check_captcha','Incorrect Security Image Response');
            return false;
        }
        
    }
}
?>